﻿var page = 1;
CmnTab = Class.create();

CmnTab.prototype = Object.extend(new Common(), {
    initialize: function () {
        this.initCommonProperty();

        this.tabList = {};

        this.tabDivList = {};

        this.dataOverCallback = null;

        this.activeMenuID = 0;
    },

    init: function () {
    },

    addTab: function (menuModel) {
        var cmnTabIns = this;

        if ($j("#" + this.dom.clientID + "_sub_cmnTab_main_" + menuModel.menu_id).length > 0) {

            $j("#" + this.dom.clientID + "_cmntab_li_" + menuModel.menu_id)[0].click();

            return;
        }

        $j("#" + this.dom.cmnTab_div).find("a").removeClass("selected");
        $j("#" + this.dom.cmnTab_div).find("span").removeClass("selected");


        var tabCount = 0;
        tabCount = $j("#" + this.dom.cmnTab_div).find("li").length
        $j("#" + this.dom.cmnTab_div).find("li").eq(tabCount - 1).before("<li id=\"" + this.dom.clientID + "_cmntab_li_" + menuModel.menu_id + "\" menu_id=\"" + menuModel.menu_id + "\" ><a class=\"selected\" style=\"cursor:pointer\"><span class=\"selected\">" + menuModel.menu_name + "</span></a></li>");
        $(this.dom.cmntab_li_0).style.display = "none";


        $j("#" + this.dom.cmnTab_div).find("li").click(function () {
            cmnTabIns.cmnTabChange(this);
        });

        $j("#" + this.dom.cmnTab_div).find("li").dblclick(function () {
            cmnTabIns.cmnTabRemove(this);
        });

        $j("#" + this.dom.cmnTab_main).append("<div id=\"" + this.dom.clientID + "_sub_cmnTab_main_" + menuModel.menu_id + "\" class=\"sub_cmnTab_main\" style='height:100%'></div>");

        $j(".sub_cmnTab_main").css("display", "none");

        $(this.dom.clientID + "_sub_cmnTab_main_" + menuModel.menu_id).style.display = "";

        $j("#" + this.dom.clientID + "_sub_cmnTab_main_" + menuModel.menu_id).append("<iframe src=\"/" + menuModel.control + "/" + menuModel.action + "/\" style=\"width:100%; height:100%;\" frameborder=0/>");

        this.activeMenuID = menuModel.menu_id;

    },

    cmnTabChange: function (obj) {
        $j(".sub_cmnTab_main").css("display", "none");

        $j("#" + this.dom.cmnTab_div).find("a").removeClass("selected");
        $j("#" + this.dom.cmnTab_div).find("span").removeClass("selected");

        $j(obj).find("a").addClass("selected");
        $j(obj).find("a").find("span").addClass("selected");
        $(obj).style.display = "";

        var menu_id = $j(obj).attr("menu_id");

        if (menu_id != undefined) {
            $(this.dom.clientID + "_sub_cmnTab_main_" + menu_id).style.display = "";

            this.activeMenuID = menu_id;
        }
    },

    cmnTabRemove: function (obj) {
        var menu_id = $j(obj).attr("menu_id");

        if ($j("#" + this.dom.clientID + "_cmntab_li_" + menu_id + " a").hasClass("selected")) {
            var prevTab = $j("#" + this.dom.clientID + "_cmntab_li_" + menu_id).prev();

            var nextTab = $j("#" + this.dom.clientID + "_cmntab_li_" + menu_id).next();

            if (prevTab.length > 0 && prevTab.css("display") != "none") {
                $j("#" + this.dom.cmnTab_div).find("a").removeClass("selected");
                $j("#" + this.dom.cmnTab_div).find("span").removeClass("selected");
                $j(prevTab).find("a").addClass("selected");
                $j(prevTab).find("a").find("span").addClass("selected");
                $j(prevTab)[0].click();
            }
            else if (nextTab.length > 0 && !nextTab.hasClass("tabspace")) {
                $j("#" + this.dom.cmnTab_div).find("a").removeClass("selected");
                $j("#" + this.dom.cmnTab_div).find("span").removeClass("selected");
                $j(nextTab).addClass("selected");
                $j(nextTab).find("span").addClass("selected");
                $j(nextTab)[0].click();
            }
            else if (prevTab.length > 0) {
                $j("#" + this.dom.cmnTab_div).find("a").removeClass("selected");
                $j("#" + this.dom.cmnTab_div).find("span").removeClass("selected");
                $j(prevTab).addClass("selected");
                $j(prevTab).find("span").addClass("selected");
                $j(prevTab)[0].click();
            }
        }

        $j("#" + this.dom.cmnTab_div).find("li").remove("#" + this.dom.clientID + "_cmntab_li_" + menu_id);
        $j("#" + this.dom.clientID + "_cmnTab_main .sub_cmnTab_main").remove("#" + this.dom.clientID + "_sub_cmnTab_main_" + menu_id);
    },

    setCmnTabBodyHeight: function (height) {
        $j("#" + this.dom.cmnTab_main).css("height", height + "px");
    },
    addTabLiCountCallback: function () {
        var clickLiPosition = 0;
        var tab_show = $j("#" + this.dom.cmnTab_div);
        var selectA = tab_show.find("a.selected").parent("li").attr("id");
        var len = tab_show.find("li").length - 1;
        for (var i = 0; i < len; i++) {
            var nowliCount = tab_show.find("li:eq(" + i + ")").attr("id");
            if (nowliCount == selectA) {
                clickLiPosition = i + 1;
            }
        }

        var liwidth = (len * 90) + (len - 1);
        tab_show.width(liwidth);

        if (!tab_show.is(":animated")) {
            if (len > 13) {
                var tabPage = Math.ceil(clickLiPosition / 13);
                if (tabPage > 1) {
                    tab_show.animate({ left: '-' + 1118 * (tabPage - 1) }, "slow");
                    page = tabPage;
                }
                else {
                    tab_show.animate({ left: '-' + 0 }, "slow");
                    page = tabPage;
                }
            }
        }
    }


});
